package com.zc.service.impl;

import com.zc.mapper.SysResourceMapper;
import com.zc.service.SysResourceService;
import com.zc.dto.SysResourceDto;
import com.zc.dto.TreeSysResourceDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysResourceServiceImpl implements SysResourceService {
    @Autowired
    private SysResourceMapper sysResourceMapper;

    @Override
    public List<SysResourceDto> listSysResourceByRoleId(Long roleId) {

        //查詢一级菜单
        Map<String, Long> map = new HashMap<>();
        map.put("roleId", roleId);
        map.put("parentId", null);
        List<SysResourceDto> sysResourceVoList = sysResourceMapper.listSysResourceByRoleId(map);

        //查詢二级菜单
        for (SysResourceDto sysResourceVo : sysResourceVoList) {
            Map<String, Long> mapSub = new HashMap<>();
            mapSub.put("roleId", roleId);
            mapSub.put("parentId", sysResourceVo.getResourceId());
            List<SysResourceDto> sysResourceVoListSub = sysResourceMapper.listSysResourceByRoleId(mapSub);
            sysResourceVo.setSubSysResourceVoList(sysResourceVoListSub);
        }
        return sysResourceVoList;
    }

    @Override
    public List<TreeSysResourceDto> listSysResource(Long roleId) {

        List<TreeSysResourceDto> sysResourceVoList =null;

        if(null == roleId){
            //查詢一级菜单
            Map<String, Long> map = new HashMap<>();
            map.put("parentId", null);
            sysResourceVoList = sysResourceMapper.listSysResource(map);

            //查詢二级菜单
            for (TreeSysResourceDto sysResourceVo : sysResourceVoList) {
                Map<String, Long> mapSub = new HashMap<>();
                mapSub.put("parentId", sysResourceVo.getId());
                List<TreeSysResourceDto> sysResourceVoListSub = sysResourceMapper.listSysResource(mapSub);
                sysResourceVo.setChildren(sysResourceVoListSub);
            }
        }else {
            //查詢一级菜单
            Map<String, Long> map = new HashMap<>();
            map.put("parentId", null);
            sysResourceVoList = sysResourceMapper.listSysResource(map);

            //查詢二级菜单
            for (TreeSysResourceDto sysResourceVo : sysResourceVoList) {
                Map<String, Long> mapSub = new HashMap<>();
                mapSub.put("parentId", sysResourceVo.getId());
                //查询所有子菜单
                List<TreeSysResourceDto> sysResourceVoListSubAll = sysResourceMapper.listSysResource(mapSub);
                //查询该角色选中的子菜单
                mapSub.put("roleId", roleId);
                List<TreeSysResourceDto> sysResourceVoListSubChecked = sysResourceMapper.listSysResource(mapSub);
                //将选中的子菜单设置为true
                for (TreeSysResourceDto sysResourceVoSubAll:sysResourceVoListSubAll) {
                    for (TreeSysResourceDto sysResourceVoSubChecked: sysResourceVoListSubChecked) {
                        if(sysResourceVoSubAll.getId().equals(sysResourceVoSubChecked.getId())){
                            sysResourceVoSubAll.setChecked(true);
                        }
                    }
                }
                sysResourceVo.setChildren(sysResourceVoListSubAll);
            }
        }

        return sysResourceVoList;
    }


}